package com.hrt.datasender.datastorage.strategy.monitordata;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.model.IModel;
import com.hrt.datasender.datadao.model.BcAccessParamModel;
import com.hrt.datasender.datadao.model.BcAccessTableModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcAccessParamService;
import com.hrt.datasender.datadao.service.BcAccessTableService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.db.DBManager;

/**
 * 
 * <pre>
 * 获取ACCESS库小时数据策略
 * </pre>
 * 
 * @author FAN
 * @date 2018年9月17日下午3:22:25
 */
public class S1HOURACCESS extends MonitorDataStrategy {
    private final int dataInterval = 3600;

    @Override
    public void registerClearShedule() {
        // TODO Auto-generated method stub

    }

    @Override
    protected Long getReadySendDataTime(BcSendPlatformModel platform) {

        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(platform);

        SQLParameterList pc = new SQLParameterList();

        Date startDate = getLastSendTime(platform);

        long firstDate = 0;

        String sql = "SELECT * FROM " + models.getHourtable() + " WHERE " + models.getDatatime()
                + " > @startDate ORDER BY " + models.getDatatime() + " ASC LIMIT 0,1";

        pc.put("startDate", startDate);

        List<Row> list = DBManager.getAccessExecuter().queryList(sql, pc);

        if (list.size() < 1) {
            return null;
        }

        Date datetime = list.get(0).getDate("DATATIME");

        firstDate = datetime.getTime();

        return firstDate;
    }

    @Override
    public List<Row> getData(BcSendPlatformModel sendPlatForm, Long readySendDatatime) {
        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(sendPlatForm);
        SQLParameterList pc = new SQLParameterList();
        Date datetime = new Date(readySendDatatime);
        String sql = "SELECT * FROM " + models.getHourtable() + " WHERE " + models.getDatatime()
                + " >= @datetime ORDER BY " + models.getDatatime() + " ASC LIMIT 0,1";
        pc.put("datetime", datetime);
        List<Row> result = DBManager.getAccessExecuter().queryList(sql, pc);

        List<Row> dataResult  = convertAccessData(result,models);
        return dataResult;
    }

    @Override
    public List<Row> getData(BcSendPlatformModel sendPlatForm, Long readySendDatatime,Long endTimeL) {
        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(sendPlatForm);
        SQLParameterList pc = new SQLParameterList();
        Date datetime = new Date(readySendDatatime);
        Date endTime = new Date(endTimeL);

        String sql = "SELECT top 1 * FROM " + models.getHourtable() + " WHERE " + models.getDatatime()
                + " >= @datetime and " + models.getDatatime() +" <= @endtime "
                + " ORDER BY " + models.getDatatime() + " ASC";
        pc.put("datetime", datetime);
        pc.put("endtime",endTime);
        List<Row> result = DBManager.getAccessExecuter().queryList(sql, pc);
        List<Row> dataResult = convertAccessData(result,models);
        return dataResult;
    }

    @Override
    public List<IModel> getStorageData(Calendar cal,List<String> parmList) {
        return null;
    }

    @Override
    public String getStoageTable() {
        return "BD_S1_HOUR";
    }

    @Override
    public String getNote() {
        return "";
    }

    @Override
    public String getShowName() {
        return "ACCESS小时数据";
    }

    @Override
    public StorageDataType getDataType() {
        return StorageDataType.HOUR;
    }

    @Override
    public int getDataInterval() {
        // TODO Auto-generated method stub
        return dataInterval;
    }

    @Override
    public String getConsStr() {
        return "0 0 0/1 * * ?";
    }

}
